package test;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class CardsPanel extends JPanel{
	private List<File> cards;
	private Image img;
	public CardsPanel (String imgPath) {
		img= new ImageIcon(cards.get(0).getAbsolutePath()).getImage();
	}
	public void paintCards(){
		Image img;
		Graphics g = null;
		for (int i = 0; i < cards.size(); i++) {
			img=new ImageIcon(cards.get(i).getAbsolutePath()).getImage();
			Graphics2D g2 = (Graphics2D) g;
			//panel width height
			int pw=this.getWidth();
			int ph=this.getHeight();
			//img width height
			int iw=img.getWidth(null);
			int ih=img.getHeight(null);
//			img缩放后的宽度和高度
			int w=iw*ph/ih;
			int h = ih*pw/iw;
			//确定以高，宽中的哪个缩放
			if(w>pw){
				w=pw;
				g2.drawImage(img,i*10,0,w,h,null);
			}else if(h>ph){
				h=ph;
				g2.drawImage(img,i*10,0,w,h,null);
			}
		}
	}
	public void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		//panel width height
		int pw=this.getWidth();
		int ph=this.getHeight();
		//img width height
		int iw=img.getWidth(null);
		int ih=img.getHeight(null);
//		img缩放后的宽度和高度
		int w=iw*ph/ih;
		int h = ih*pw/iw;
		//确定以高，宽中的哪个缩放
		if(w>pw){
			w=pw;
			g2.drawImage(img,0,0,w,h,null);
		}else if(h>ph){
			h=ph;
			g2.drawImage(img,0,0,w,h,null);
		}
	}
}
